package com.zzptc.myapplication1015;

import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;

import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

import com.zzptc.myapplication1015.entiy.User;

import java.util.ArrayList;
import java.util.List;

public class MainActivity extends AppCompatActivity {
     Button denglu;
     Button zhuce;
    EditText  zhanghao;
    EditText mima;
    private static final String TAG = "MainActivity";//logt+Tab键
    List<User> list=new ArrayList<>();

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        init();//实例化控件
        event();//处理事件
    }
    private void event() {
        //注册
          zhuce.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
               // Toast.makeText(MainActivity.this,"已经注册",Toast.LENGTH_SHORT).show();
                Intent intent=new Intent(MainActivity.this,Zhuce2Activity.class);//跳转
                startActivityForResult(intent,1);
            }
        });
          denglu.setOnClickListener (new View.OnClickListener() {
              @Override
              public void onClick(View v) {


              }
          });
    }
    private void init() {
      denglu=findViewById(R.id.denglu);
      zhuce=findViewById(R.id.zhuce);
      zhanghao=findViewById(R.id.zhanghao);
      mima=findViewById(R.id.mima);
    }
    @Override
    protected void onDestroy() {
        super.onDestroy();
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {
        super.onActivityResult(requestCode, resultCode, data);//回传给谁
       if (requestCode==1&&requestCode==2){
       //取值
       String name=data.getStringExtra("name");
       String pwd=data.getStringExtra("pwd");
       Log.d(TAG, "转回值是："+name);
       Log.d(TAG, "转回值是："+pwd);
       //Toast.makeText(this,""+zhanghao,Toast.LENGTH_SHORT).show();
       //封装数据
           User user=new User(name,pwd);
           //注册
          reg(user);
       }
    }
    public void reg(User user){
//        //判断集合中有没有对象或有没有该对象
//        if (list.size()==0){
//            list.add(user);//注册
//            Log.e(TAG, list.size()+"---"+user.getUsername());
//        }else {
//            for (User s:list){
//                if (s.getUsername().equals(user.getUsername())){
//                    Toast.makeText(this,"该用户已经存在",Toast.LENGTH_SHORT).show();
//                    break;
//                }else {
//                    list.add(user);
 //       Log.d(TAG, "reg: =========="+user.getUsername());
//                }
//            }
//        }
        //获取SharedPreferences对象
        SharedPreferences sp = getSharedPreferences("user", MODE_PRIVATE);
        //存入数据
        SharedPreferences.Editor editor = sp.edit(); //获取到edit对象
        editor.putString("姓名", user.getUsername());
        editor.putString("密码", user.getPassword());

        //  editor.putInt("年龄", Integer.parseInt(user.getPassword()));
        //editor.putBoolean("性别", Boolean.parseBoolean("男"));
        editor.commit();//提交数据

        //返回STRING_KEY的值
        Log.d("SP", sp.getString("姓名", "none"));
        //如果NOT_EXIST不存在，则返回值为"none"
        Log.d("SP", sp.getString("NOT_EXIST", "none"));
    }
}
